
var mysql = require('mysql');
var connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '123456',
    database: 'hellowaimai'
});
connection.connect();

const Headers = {
    "Cookie": "_ga=GA1.2.1217262868.1636940744; _gid=GA1.2.822822965.1636940744; _gat=1; Hm_lvt_cdb524f42f0ce19b169a8071123a4797=1636940744,1637023524,1637023549,1637023553; Hm_lpvt_cdb524f42f0ce19b169a8071123a4797=1637023553; kw_token=3REKQ0SW4D",
    "csrf": "3REKQ0SW4D"
}

const request = require("request")
function yyy (url_str) {
    return new Promise(function (chenggong, shibai) {
        request({
            url: url_str,
            headers: Headers
        }, function (error, response, data) {
            if (error) {
                shibai(error)
            } else {
                chenggong(data)
            }
        })
    })
}


function xxx(sql_str) {
    return new Promise(function (reslove, shibai) {
        // 执行 sql 语句
        connection.query(sql_str, function (error, results, fields) {
            if (error != null) {
                console.log(error)
                var tmp_dic = {
                    error: 1,
                    msg: "执行失败",
                    data: []
                }
                reslove(JSON.stringify(tmp_dic))
            } else {
                var tmp_dic = {
                    error: 0,
                    msg: "执行成功",
                    data: results
                }
                // // return JSON.stringify(results)
                // reslove(tmp_dic.data)

                reslove(JSON.stringify(tmp_dic))
            }
        });
    })
}


const error_info = (msg, error) => {
    return {
        err_code: 1,
        msg: msg,
        error: error
    }
}
const success_info = (msg, data) => {
    return {
        err_code: 0,
        msg: msg,
        data: data
    }
}

exports.xxx = xxx
exports.yyy = yyy
exports.error_info = error_info
exports.success_info = success_info